package com.youedata.modular.controller;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youedata.base.pojo.page.LayuiPageFactory;
import com.youedata.base.tips.DCResponse;
import com.youedata.modular.entity.WaterCutOffNoticeInfo;
import com.youedata.modular.model.dto.WaterCutOffNoticeInfoDTO;
import com.youedata.modular.service.IWaterCutOffNoticeInfoService;
import com.youedata.sys.core.util.UserHolder;
import com.youedata.util.ToolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 停水公告表控制器
 *
 * @author hst
 * @date 2021-07-26 18:38:30
 */
@Controller
@Api(value = "/waterCutOffNoticeInfo", tags = "停水公告表管理")
@RequestMapping("/waterCutOffNoticeInfo")
public class WaterCutOffNoticeInfoController {

    private String PREFIX = "/waterCutOffNoticeInfo";

    @Autowired
    private IWaterCutOffNoticeInfoService waterCutOffNoticeInfoService;

    /**
     * 新增
     *
     * @author hst
     * @date 2021-07-26
     */
    @ResponseBody
    @PostMapping("/addItem")
    @ApiOperation(value = "新增")
    public DCResponse<Void> addItem(WaterCutOffNoticeInfoDTO waterCutOffNoticeInfoDto) {
        WaterCutOffNoticeInfo waterCutOffNoticeInfo = new WaterCutOffNoticeInfo();
        BeanUtils.copyProperties(waterCutOffNoticeInfoDto, waterCutOffNoticeInfo);
        waterCutOffNoticeInfo.setCreateUser(String.valueOf(UserHolder.getUserId()));
        waterCutOffNoticeInfo.setCreateTime(LocalDateTime.now());
        this.waterCutOffNoticeInfoService.save(waterCutOffNoticeInfo);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键删除
     *
     * @author hst
     * @date 2021-07-26
     */
    @ResponseBody
    @PostMapping("/delete")
    @ApiOperation("删除")
    public DCResponse<Void> delete(@RequestParam("id") String id) {
        this.waterCutOffNoticeInfoService.removeById(id);
        return DCResponse.ok(null);
    }

    /**
     * 修改
     *
     * @author hst
     * @date 2021-07-26
     */
    @ResponseBody
    @PostMapping("/editItem")
    @ApiOperation("根据ID修改信息")
    public DCResponse<Void> editItem(WaterCutOffNoticeInfoDTO waterCutOffNoticeInfoDto) {
        WaterCutOffNoticeInfo waterCutOffNoticeInfo = new WaterCutOffNoticeInfo();
        BeanUtils.copyProperties(waterCutOffNoticeInfoDto, waterCutOffNoticeInfo);
        waterCutOffNoticeInfo.setUpdateUser(Long.valueOf(Objects.requireNonNull(UserHolder.getUserId())));
        waterCutOffNoticeInfo.setUpdateTime(LocalDateTime.now());
        this.waterCutOffNoticeInfoService.updateById(waterCutOffNoticeInfo);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键查询单条详情
     *
     * @author hst
     * @date 2021-07-26
     */
    @ResponseBody
    @PostMapping("/detail")
    @ApiOperation(value = "根据主键查询单条详情")
    public ResponseData detail(@RequestParam("id") String id) {
        return ResponseData.success(this.waterCutOffNoticeInfoService.getById(id));
    }

    /**
     * 分页查询列表
     *
     * @author hst
     * @date 2021-07-26
     */
    @ResponseBody
    @GetMapping("/list")
    @ApiOperation(value = "分页查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, defaultValue = "1", paramType = "query", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, defaultValue = "10", paramType = "query", dataType = "int", example = "20")
    })
    public Object list(WaterCutOffNoticeInfoDTO waterCutOffNoticeInfoDto) {
        WaterCutOffNoticeInfo waterCutOffNoticeInfo = new WaterCutOffNoticeInfo();

        waterCutOffNoticeInfoDto.setType(StringUtils.isEmpty(waterCutOffNoticeInfoDto.getType()) ? null : waterCutOffNoticeInfoDto.getType());

        BeanUtils.copyProperties(waterCutOffNoticeInfoDto, waterCutOffNoticeInfo);

        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();

        String type = waterCutOffNoticeInfoDto.getType();
        //根据条件查询
        //page.setRecords(this.waterCutOffNoticeInfoService.page(page, Wrappers.query(waterCutOffNoticeInfo)).getRecords());

        List<WaterCutOffNoticeInfo> waterCutOffNoticeInfoList = this.waterCutOffNoticeInfoService.queryTypeFuzzy(page, type,waterCutOffNoticeInfoDto.getNoticeTitle());
        if (ObjectUtils.isNotEmpty(waterCutOffNoticeInfoList)) {
            waterCutOffNoticeInfoList.stream().map(x -> {
                if (ToolUtil.isNotEmpty(x.getCreateTime())) {
                    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
                    x.setCreateTimeStr(dtf.format(x.getCreateTime()));
                }
                return x;
            }).collect(Collectors.toList());
        }
        page.setRecords(waterCutOffNoticeInfoList);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 跳转到主页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping()
    public String index() {
        return PREFIX + "/waterCutOffNoticeInfo.html";
    }

    /**
     * 新增页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/waterCutOffNoticeInfo_add.html";
    }

    /**
     * 编辑页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/waterCutOffNoticeInfo_edit.html";
    }

    /**
     * 跳转到主页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/sjxx")
    public String sjxx() {
        return PREFIX + "/sjxx/sjxx.html";
    }

    /**
     * 新增页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/sjxx/add")
    public String sjxxAdd() {
        return PREFIX + "/sjxx/sjxx_add.html";
    }

    /**
     * 编辑页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/sjxx/edit")
    public String sjxxEdit() {
        return PREFIX + "/sjxx/sjxx_edit.html";
    }

    /**
     * 跳转到主页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/yscs")
    public String ycss() {
        return PREFIX + "/yscs/yscs.html";
    }

    /**
     * 新增页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/yscs/add")
    public String yscsAdd() {
        return PREFIX + "/yscs/yscs_add.html";
    }

    /**
     * 编辑页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/yscs/edit")
    public String ycssEdit() {
        return PREFIX + "/yscs/yscs_edit.html";
    }

    /**
     * 跳转到主页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/tsgg")
    public String tsgg() {
        return PREFIX + "/tsgg/tsgg.html";
    }

    /**
     * 新增页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/tsgg/add")
    public String tsggAdd() {
        return PREFIX + "/tsgg/tsgg_add.html";
    }

    /**
     * 编辑页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/tsgg/edit")
    public String tsggEdit() {
        return PREFIX + "/tsgg/tsgg_edit.html";
    }

    /**
     * 跳转到主页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/szgs")
    public String szgs() {
        return PREFIX + "/szgs/szgs.html";
    }

    /**
     * 新增页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/szgs/add")
    public String szgsAdd() {
        return PREFIX + "/szgs/szgs_add.html";
    }

    /**
     * 编辑页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/szgs/edit")
    public String szgsEdit() {
        return PREFIX + "/szgs/szgs_edit.html";
    }
}
